package com.justgame.project.base.config;

import com.justgame.project.cloud.common.util.HttpContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;


/**
 * 配置Websocket的配置类
 **/
@Slf4j
@Configuration
public class WebSocketConfig extends ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
        HttpSession session = (HttpSession) request.getHttpSession();
        if (session != null) {
            config.getUserProperties().put(HttpSession.class.getName(), session);
            log.info("SessionID：{}  |||  IP：{}", session.getId(), HttpContextUtil.getIp());
        } else {
            super.modifyHandshake(config, request, response);
        }
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}
